package tt.dz.egobus.servicemanager.service.impl;

import org.springframework.stereotype.Service;
import tt.dz.egobus.api.ResponseBack;
import tt.dz.egobus.api.page.Page;
import tt.dz.egobus.api.page.Pageable;
import tt.dz.egobus.api.usercenter.DeleteBack;
import tt.dz.egobus.api.usercenter.staff.StaffBack;
import tt.dz.egobus.servicemanager.base.service.AbstractFacadeServiceConfig;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactory;
import tt.dz.egobus.servicemanager.facade.interator.FacadeFactoryLoggerInterceptorHolder;
import tt.dz.egobus.servicemanager.facade.user.StaffFacade;
import tt.dz.egobus.servicemanager.service.DriverManagerService;
import tt.dz.egobus.vo.KV;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author Anson
 * @date 17-4-1
 * @name 基于dubbo调用实现的司机管理的CURD
 * @since 1.0.3
 */
@Service("driverManagerDubboServiceImpl")
public class DubboDriverManagerServiceImpl extends AbstractFacadeServiceConfig implements DriverManagerService {

    @Override
    @PostConstruct
    protected FacadeFactory getFacadeFactory() {
        return FacadeFactoryLoggerInterceptorHolder.LOGGER_FACADE;
    }


    private StaffFacade getStaffApiService() {
        return this.facade("staff", StaffFacade.class);
    }

    @Override
    public ResponseBack<List<StaffBack>> queryAll() {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<List<StaffBack>> responseBack = staffFacade.queryAllStaff();
        return responseBack;
    }

    @Override
    public ResponseBack<Boolean> judgeStaffExists(String idcard) {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<Boolean> responseBack = staffFacade.judgeStaffExists(idcard);
        return responseBack;
    }

    @Override
    public ResponseBack<StaffBack> createStaff(String realName, String idCard) {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<StaffBack> responseBack = staffFacade.createStaff(realName, idCard);
        return responseBack;
    }

    @Override
    public ResponseBack<Page<StaffBack>> queryStaffPage(Pageable pageable) {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<Page<StaffBack>> responseBack = staffFacade.queryStaffPage(pageable);
        return responseBack;
    }

    @Override
    public ResponseBack<DeleteBack> deleteStaff(String[] ids) {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<DeleteBack> responseBack = staffFacade.deleteStaff(ids);
        return responseBack;
    }

    @Override
    public ResponseBack<StaffBack> updataStaff(String sId, String realName, String idCard) {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<StaffBack> responseBack = staffFacade.updataStaff(sId, realName, idCard);
        return responseBack;
    }

    @Override
    public KV<Boolean,String> deleteStaff(String id) {
        StaffFacade staffFacade = this.getStaffApiService();
        ResponseBack<DeleteBack> responseBack = staffFacade.deleteStaff(new String[]{id});
        boolean isSuccess = responseBack.isSuccess();
        DeleteBack deleteBack = responseBack.getResult();
        String errMsg ="";
        if (isSuccess) {
            isSuccess = deleteBack != null && deleteBack.getIsSucced() && Integer.valueOf(0).compareTo(deleteBack.getErrorNumber()) == 0;
        }
        errMsg = deleteBack!=null ? deleteBack.getMessage() : "";
        KV<Boolean,String> kv = new KV<Boolean,String>(isSuccess,errMsg);
        return kv;
    }
}
